﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yu.Cms.Common;

namespace Yu.Cms.Web.UserCtrl.Default
{
    public partial class CommentC : BaseCtrl
    {
        private Yu.Enum.CTypeID _commenttype;
        private string _returnurl;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string act = YuRequest.GetQueryString("act",true);
                if (act.ToLower() == "respond")
                {
                    string author = YuRequest.GetFormString("author");
                    string email = YuRequest.GetFormString("email");
                    string url = YuRequest.GetFormString("url");
                    string comment = YuRequest.GetFormString("comment");
                    int comment_post_ID = YuRequest.GetFormInt("comment_post_ID",0);
                    int comment_parent = YuRequest.GetFormInt("comment_parent",0);
                    if (comment_post_ID == 0)
                    {
                        Response.Write("1");
                        Response.End();
                    }
                    if (this.AddComment(author, email, url, comment, comment_post_ID, comment_parent))
                    {
                        Response.Write("0");
                        Response.End();
                    }
                }
                int PostID = YuRequest.GetQueryInt("PostID", 0);
                this.OnStart(PostID);
            }
        }

        /// <summary>
        /// 判断是否有评论
        /// </summary>
        /// <param name="PostID"></param>
        protected void OnStart(int PostID)
        {
            if (bllComment.isCommentByPostID(PostID, CommentType))
            {
                ph.Controls.Add(LoadControl("CommentYes.ascx"));
            }
            else
            {
                ph.Controls.Add(LoadControl("CommentNo.ascx"));
            }           
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        protected bool AddComment(string author, string email, string url, string comment, int comment_post_ID,int comment_parent)
        {
            Yu.Cms.Model.Comment mComment = new Yu.Cms.Model.Comment();
            mComment.TypeID = (int)CommentType;
            mComment.PostID = comment_post_ID;
            mComment.ParentID = comment_parent;
            mComment.CommentTitle = author;
            mComment.CommentEmail = email;
            mComment.CommentUrl = url;
            mComment.bComment = comment;
            mComment.PubIp = Utils.GetRealIP();
            mComment.PubTime = DateTime.Now;
            int rInt = 0;
            rInt = bllComment.Add(mComment);
            if (rInt != -1)
            {
                this.UpdateComment(comment_post_ID);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 增加统计评论次数
        /// </summary>
        /// <param name="PostID"></param>
        protected void UpdateComment(int PostID)
        {
            if (CommentType == Yu.Enum.CTypeID.Pic) //相册
            {
                bllalbumType.UpdateAlumComment(PostID,Yu.Enum.AddSub.Add);
            }
            else if (CommentType == Yu.Enum.CTypeID.Artice) //文章
            {
                bllContent.UpdateContentComment(PostID, Yu.Enum.AddSub.Add);
            }
        }

        /// <summary>
        /// 返回地址
        /// </summary>
        public string ReturnUrl
        {            
            get {
                if (CommentType.Equals(Yu.Enum.CTypeID.Artice))
                {
                    _returnurl = "/Article.aspx?PostID=";
                }
                else if (CommentType.Equals(Yu.Enum.CTypeID.Pic))
                {
                    _returnurl = "/AlbumList.aspx?PostID=";
                }
                else
                {
                    _returnurl=string.Empty ;
                }
                return _returnurl;
            }            
        }


        /// <summary>
        /// 返回评论类型
        /// </summary>
        public Yu.Enum.CTypeID CommentType
        {
            set { _commenttype = value; }
            get { return _commenttype; }
        }
    }
}